package com.littlebee.storysuper.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.activity.MainActivity;
import com.littlebee.storysuper.activity.RegisterActivity;
import com.littlebee.storysuper.common.sharePrefUtils.SharePrefUtil;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.LogUtil;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.ToastUtil;
import com.littlebee.storysuper.utils.Utils;
import com.zss.library.utils.LogUtils;
import com.zss.library.utils.SharedPrefUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginFragment extends com.zss.library.fragment.BaseFragment implements View.OnClickListener {
    public EditText et_username;
    public EditText et_password;
    public Button submit;
    private User user;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_login;
    }

    @Override
    public void initView() {
        super.initView();
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        submit = (Button) findViewById(R.id.btn_submit);
        submit.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);


    }

    public void onSubmitBtnClicked() {
        String phoneStr = et_username.getText().toString().trim();
        String pwdStr = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(phoneStr)) {
            ToastUtil.displayShortToast(R.string.please_input_phone);

            return;
        }
        if (TextUtils.isEmpty(pwdStr)) {
            ToastUtil.displayShortToast(R.string.please_input_password);

            return;
        }
        SharedPrefUtils prefUtils = Utils.getSharedPrefCommonFile();
        String pushId = prefUtils.get("pushId", "");
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("phoneId", phoneStr);
        hashMap.put("password", pwdStr);
        hashMap.put("pushId", pushId);
        loadData(hashMap);
    }

    /*
    * http请求的调用汇总
    * */
    public void loadData(Map<String, String> hashParams) {
        final String phone = hashParams.get("phoneId");
        final String pass = hashParams.get("password");
        // TODO: 2017/8/14
        NetWorkUtils.get(getActivity(), "login", hashParams, true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                LogUtils.i("XHX", "XHXSUCC:" + result);
                bingMessage(resObj, phone, pass);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);

            }
        });
    }

    /**
     * 登录极光IM
     */
    private void bingMessage(final JSONObject resObj, final String userName, final String password) {
        User user = new User();
        user.setMobile(userName);
        user.setPassword(password);
        user.setRole(1);


        SharePrefUtil.getInstance(getContext()).setObject(SharePrefUtil.kAccountInfo, user);
        if(RegisterActivity.isFirst==true){
            LogUtil.d("liaoyong:bind message, goto mainactivity");
            Intent intent=new Intent(getActivity(),MainActivity.class);
            startActivity(intent);
            getActivity().finish();
        }else{
            LogUtil.d("liaoyong:bind message, finish this...");
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
        }



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                onSubmitBtnClicked();
                break;
        }
    }
}
